<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <meta name="description" content=""/>

    <meta name="twitter:card" content="summary"/>
    <meta name="twitter:image:src" content="https://avatars1.githubusercontent.com/u/64068543?s=400&amp;v=4"/>
    <meta name="twitter:title" content="experiment.py"/>
    <meta name="twitter:description" content=""/>
    <meta name="twitter:site" content="@labmlai"/>
    <meta name="twitter:creator" content="@labmlai"/>

    <meta property="og:url" content="https://nn.labml.ai/rwkv/experiment.html"/>
    <meta property="og:title" content="experiment.py"/>
    <meta property="og:image" content="https://avatars1.githubusercontent.com/u/64068543?s=400&amp;v=4"/>
    <meta property="og:site_name" content="experiment.py"/>
    <meta property="og:type" content="object"/>
    <meta property="og:title" content="experiment.py"/>
    <meta property="og:description" content=""/>

    <title>experiment.py</title>
    <link rel="shortcut icon" href="/icon.png"/>
    <link rel="stylesheet" href="../pylit.css?v=1">
    <link rel="canonical" href="https://nn.labml.ai/rwkv/experiment.html"/>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.13.18/dist/katex.min.css" integrity="sha384-zTROYFVGOfTw7JV7KUu8udsvW2fx4lWOsCEDqhBreBwlHI4ioVRtmIvEThzJHGET" crossorigin="anonymous">

    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-4V3HC8HBLH"></script>
    <script>
        window.dataLayer = window.dataLayer || [];

        function gtag() {
            dataLayer.push(arguments);
        }

        gtag('js', new Date());

        gtag('config', 'G-4V3HC8HBLH');
    </script>
</head>
<body>
<div id='container'>
    <div id="background"></div>
    <div class='section'>
        <div class='docs'>
            <p>
                <a class="parent" href="/">home</a>
                <a class="parent" href="index.html">rwkv</a>
            </p>
            <p>
                <a href="https://github.com/labmlai/annotated_deep_learning_paper_implementations" target="_blank">
                    <img alt="Github"
                         src="https://img.shields.io/github/stars/labmlai/annotated_deep_learning_paper_implementations?style=social"
                         style="max-width:100%;"/></a>
                <a href="https://twitter.com/labmlai" rel="nofollow" target="_blank">
                    <img alt="Twitter"
                         src="https://img.shields.io/twitter/follow/labmlai?style=social"
                         style="max-width:100%;"/></a>
            </p>
            <p>
                <a href="https://github.com/labmlai/annotated_deep_learning_paper_implementations/tree/master/labml_nn/rwkv/experiment.py" target="_blank">
                    View code on Github</a>
            </p>
        </div>
    </div>
    <div class='section' id='section-0'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-0'>#</a>
            </div>
            
        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">1</span><span></span><span class="kn">import</span> <span class="nn">inspect</span>
<span class="lineno">2</span><span class="kn">import</span> <span class="nn">math</span>
<span class="lineno">3</span>
<span class="lineno">4</span><span class="kn">import</span> <span class="nn">torch</span>
<span class="lineno">5</span><span class="kn">import</span> <span class="nn">torch.nn</span> <span class="k">as</span> <span class="nn">nn</span>
<span class="lineno">6</span><span class="kn">from</span> <span class="nn">labml_nn.rwkv.configs</span> <span class="kn">import</span> <span class="n">RWKVConfigs</span>
<span class="lineno">7</span>
<span class="lineno">8</span><span class="kn">from</span> <span class="nn">labml_nn.rwkv</span> <span class="kn">import</span> <span class="n">RWKV</span>
<span class="lineno">9</span><span class="kn">from</span> <span class="nn">labml_nn.rwkv</span> <span class="kn">import</span> <span class="n">TimeMixing</span>
<span class="lineno">10</span><span class="kn">from</span> <span class="nn">labml</span> <span class="kn">import</span> <span class="n">experiment</span>
<span class="lineno">11</span><span class="kn">from</span> <span class="nn">labml.configs</span> <span class="kn">import</span> <span class="n">option</span>
<span class="lineno">12</span><span class="kn">from</span> <span class="nn">labml_nn.experiments.nlp_autoregression</span> <span class="kn">import</span> <span class="n">NLPAutoRegressionConfigs</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-1'>
        <div class='docs doc-strings'>
            <div class='section-link'>
                <a href='#section-1'>#</a>
            </div>
            <h2>Configurations</h2>
<p>This inherits from <a href="../../experiments/nlp_autoregression.html#NLPAutoRegressionConfigs"><code  class="highlight"><span></span><span class="n">NLPAutoRegressionConfigs</span></code>
</a></p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">15</span><span class="k">class</span> <span class="nc">Configs</span><span class="p">(</span><span class="n">NLPAutoRegressionConfigs</span><span class="p">):</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-2'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-2'>#</a>
            </div>
            <p>RWKV model </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">24</span>    <span class="n">model</span><span class="p">:</span> <span class="n">RWKV</span>
<span class="lineno">25</span>
<span class="lineno">26</span>    <span class="n">rwkv</span><span class="p">:</span> <span class="n">RWKVConfigs</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-3'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-3'>#</a>
            </div>
            <p>number of warmup iterations </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">28</span>    <span class="n">warmup_iters</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">2000</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-4'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-4'>#</a>
            </div>
            <p>total number of training iterations </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">30</span>    <span class="n">max_iters</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">600000</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-5'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-5'>#</a>
            </div>
            <p>weight decay </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">32</span>    <span class="n">weight_decay</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">1e-1</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-6'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-6'>#</a>
            </div>
            <p>Custom optimizer </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">34</span>    <span class="n">beta1</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">0.9</span>
<span class="lineno">35</span>    <span class="n">beta2</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">0.95</span>
<span class="lineno">36</span>    <span class="n">optimizer</span> <span class="o">=</span> <span class="s1">&#39;rwkv_optimizer&#39;</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-7'>
        <div class='docs doc-strings'>
            <div class='section-link'>
                <a href='#section-7'>#</a>
            </div>
            <h3>RWKV configurations</h3>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">39</span><span class="nd">@option</span><span class="p">(</span><span class="n">Configs</span><span class="o">.</span><span class="n">rwkv</span><span class="p">,</span> <span class="s1">&#39;RWKV&#39;</span><span class="p">)</span>
<span class="lineno">40</span><span class="k">def</span> <span class="nf">_rwkv_configs</span><span class="p">(</span><span class="n">c</span><span class="p">:</span> <span class="n">Configs</span><span class="p">):</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-8'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-8'>#</a>
            </div>
            <p>We use our <a href="../configs.html#RWKVConfigs">configurable RWKV implementation</a> </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">47</span>    <span class="n">conf</span> <span class="o">=</span> <span class="n">RWKVConfigs</span><span class="p">()</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-9'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-9'>#</a>
            </div>
            <p>Set the vocabulary sizes for embeddings and generating logits </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">49</span>    <span class="n">conf</span><span class="o">.</span><span class="n">n_src_vocab</span> <span class="o">=</span> <span class="n">c</span><span class="o">.</span><span class="n">n_tokens</span>
<span class="lineno">50</span>    <span class="n">conf</span><span class="o">.</span><span class="n">n_tgt_vocab</span> <span class="o">=</span> <span class="n">c</span><span class="o">.</span><span class="n">n_tokens</span>
<span class="lineno">51</span>
<span class="lineno">52</span>    <span class="k">return</span> <span class="n">conf</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-10'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-10'>#</a>
            </div>
            
        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">55</span><span class="k">def</span> <span class="nf">_init_weights</span><span class="p">(</span><span class="n">module</span><span class="p">,</span> <span class="n">rwkv</span><span class="p">:</span> <span class="n">RWKVConfigs</span><span class="p">):</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-11'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-11'>#</a>
            </div>
            <p>initialize Vector Parameters in TimeMixing </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">57</span>    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">module</span><span class="p">,</span> <span class="n">TimeMixing</span><span class="p">):</span>
<span class="lineno">58</span>        <span class="n">layer_id</span> <span class="o">=</span> <span class="n">module</span><span class="o">.</span><span class="n">layer_id</span>
<span class="lineno">59</span>        <span class="n">n_layer</span> <span class="o">=</span> <span class="n">module</span><span class="o">.</span><span class="n">n_layer</span>
<span class="lineno">60</span>        <span class="n">n_embd</span> <span class="o">=</span> <span class="n">module</span><span class="o">.</span><span class="n">n_embd</span>
<span class="lineno">61</span>        <span class="n">attn_sz</span> <span class="o">=</span> <span class="n">n_embd</span>
<span class="lineno">62</span>
<span class="lineno">63</span>        <span class="k">with</span> <span class="n">torch</span><span class="o">.</span><span class="n">no_grad</span><span class="p">():</span>
<span class="lineno">64</span>            <span class="n">ratio_0_to_1</span> <span class="o">=</span> <span class="n">layer_id</span> <span class="o">/</span> <span class="p">(</span><span class="n">n_layer</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>  <span class="c1"># 0 to 1</span>
<span class="lineno">65</span>            <span class="n">ratio_1_to_almost0</span> <span class="o">=</span> <span class="mf">1.0</span> <span class="o">-</span> <span class="p">(</span><span class="n">layer_id</span> <span class="o">/</span> <span class="n">n_layer</span><span class="p">)</span>  <span class="c1"># 1 to ~0</span>
<span class="lineno">66</span>            <span class="n">ddd</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">n_embd</span><span class="p">)</span>
<span class="lineno">67</span>            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n_embd</span><span class="p">):</span>
<span class="lineno">68</span>                <span class="n">ddd</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">i</span> <span class="o">/</span> <span class="n">n_embd</span>
<span class="lineno">69</span>
<span class="lineno">70</span>            <span class="n">decay_speed</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="n">attn_sz</span><span class="p">)</span>
<span class="lineno">71</span>            <span class="k">for</span> <span class="n">h</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">attn_sz</span><span class="p">):</span>
<span class="lineno">72</span>                <span class="n">decay_speed</span><span class="p">[</span><span class="n">h</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="mi">5</span> <span class="o">+</span> <span class="mi">8</span> <span class="o">*</span> <span class="p">(</span><span class="n">h</span> <span class="o">/</span> <span class="p">(</span><span class="n">attn_sz</span> <span class="o">-</span> <span class="mi">1</span><span class="p">))</span> <span class="o">**</span> <span class="p">(</span><span class="mf">0.7</span> <span class="o">+</span> <span class="mf">1.3</span> <span class="o">*</span> <span class="n">ratio_0_to_1</span><span class="p">)</span>
<span class="lineno">73</span>            <span class="n">module</span><span class="o">.</span><span class="n">time_decay</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">Parameter</span><span class="p">(</span><span class="n">decay_speed</span><span class="p">)</span>
<span class="lineno">74</span>
<span class="lineno">75</span>            <span class="n">zigzag</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">tensor</span><span class="p">([(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">%</span> <span class="mi">3</span> <span class="o">-</span> <span class="mi">1</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">attn_sz</span><span class="p">)])</span> <span class="o">*</span> <span class="mf">0.5</span>
<span class="lineno">76</span>            <span class="n">module</span><span class="o">.</span><span class="n">time_first</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">Parameter</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="n">attn_sz</span><span class="p">)</span> <span class="o">*</span> <span class="n">math</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="mf">0.3</span><span class="p">)</span> <span class="o">+</span> <span class="n">zigzag</span><span class="p">)</span>
<span class="lineno">77</span>            <span class="n">module</span><span class="o">.</span><span class="n">time_mix_key</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">Parameter</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">pow</span><span class="p">(</span><span class="n">ddd</span><span class="p">,</span> <span class="n">ratio_1_to_almost0</span><span class="p">))</span>
<span class="lineno">78</span>            <span class="n">module</span><span class="o">.</span><span class="n">time_mix_value</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">Parameter</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">pow</span><span class="p">(</span><span class="n">ddd</span><span class="p">,</span> <span class="n">ratio_1_to_almost0</span><span class="p">)</span> <span class="o">+</span> <span class="mf">0.3</span> <span class="o">*</span> <span class="n">ratio_0_to_1</span><span class="p">)</span>
<span class="lineno">79</span>            <span class="n">module</span><span class="o">.</span><span class="n">time_mix_receptance</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">Parameter</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">pow</span><span class="p">(</span><span class="n">ddd</span><span class="p">,</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="n">ratio_1_to_almost0</span><span class="p">))</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-12'>
        <div class='docs doc-strings'>
            <div class='section-link'>
                <a href='#section-12'>#</a>
            </div>
            <p> Create RWKV model and initialize weights</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">82</span><span class="nd">@option</span><span class="p">(</span><span class="n">Configs</span><span class="o">.</span><span class="n">model</span><span class="p">)</span>
<span class="lineno">83</span><span class="k">def</span> <span class="nf">_model</span><span class="p">(</span><span class="n">c</span><span class="p">:</span> <span class="n">Configs</span><span class="p">):</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-13'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-13'>#</a>
            </div>
            
        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">87</span>    <span class="n">m</span> <span class="o">=</span> <span class="n">RWKV</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">rwkv</span><span class="p">)</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">device</span><span class="p">)</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-14'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-14'>#</a>
            </div>
            <p>Apply custom weight initialization </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">90</span>    <span class="n">m</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">_init_weights</span><span class="p">,</span> <span class="n">c</span><span class="o">.</span><span class="n">rwkv</span><span class="p">)</span>
<span class="lineno">91</span>
<span class="lineno">92</span>    <span class="k">return</span> <span class="n">m</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-15'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-15'>#</a>
            </div>
            
        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">95</span><span class="nd">@option</span><span class="p">(</span><span class="n">NLPAutoRegressionConfigs</span><span class="o">.</span><span class="n">optimizer</span><span class="p">)</span>
<span class="lineno">96</span><span class="k">def</span> <span class="nf">_configure_optimizers</span><span class="p">(</span><span class="n">c</span><span class="p">:</span> <span class="n">NLPAutoRegressionConfigs</span><span class="p">):</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-16'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-16'>#</a>
            </div>
            <p>start with all of the candidate parameters </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">98</span>    <span class="n">param_dict</span> <span class="o">=</span> <span class="p">{</span><span class="n">pn</span><span class="p">:</span> <span class="n">p</span> <span class="k">for</span> <span class="n">pn</span><span class="p">,</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">c</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">named_parameters</span><span class="p">()}</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-17'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-17'>#</a>
            </div>
            <p>filter out those that do not require grad </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">100</span>    <span class="n">param_dict</span> <span class="o">=</span> <span class="p">{</span><span class="n">pn</span><span class="p">:</span> <span class="n">p</span> <span class="k">for</span> <span class="n">pn</span><span class="p">,</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">param_dict</span><span class="o">.</span><span class="n">items</span><span class="p">()</span> <span class="k">if</span> <span class="n">p</span><span class="o">.</span><span class="n">requires_grad</span><span class="p">}</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-18'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-18'>#</a>
            </div>
            <p>create optim groups. Any parameters that is 2D will be weight decayed, otherwise no. i.e. all weight tensors in matmuls + embeddings decay, all biases and layernorms don&#x27;t. </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">103</span>    <span class="n">decay_params</span> <span class="o">=</span> <span class="p">[</span><span class="n">p</span> <span class="k">for</span> <span class="n">n</span><span class="p">,</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">param_dict</span><span class="o">.</span><span class="n">items</span><span class="p">()</span> <span class="k">if</span> <span class="n">p</span><span class="o">.</span><span class="n">dim</span><span class="p">()</span> <span class="o">&gt;=</span> <span class="mi">2</span><span class="p">]</span>
<span class="lineno">104</span>    <span class="n">nodecay_params</span> <span class="o">=</span> <span class="p">[</span><span class="n">p</span> <span class="k">for</span> <span class="n">n</span><span class="p">,</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">param_dict</span><span class="o">.</span><span class="n">items</span><span class="p">()</span> <span class="k">if</span> <span class="n">p</span><span class="o">.</span><span class="n">dim</span><span class="p">()</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="p">]</span>
<span class="lineno">105</span>    <span class="n">optim_groups</span> <span class="o">=</span> <span class="p">[</span>
<span class="lineno">106</span>        <span class="p">{</span><span class="s1">&#39;params&#39;</span><span class="p">:</span> <span class="n">decay_params</span><span class="p">,</span> <span class="s1">&#39;weight_decay&#39;</span><span class="p">:</span> <span class="n">c</span><span class="o">.</span><span class="n">weight_decay</span><span class="p">},</span>
<span class="lineno">107</span>        <span class="p">{</span><span class="s1">&#39;params&#39;</span><span class="p">:</span> <span class="n">nodecay_params</span><span class="p">,</span> <span class="s1">&#39;weight_decay&#39;</span><span class="p">:</span> <span class="mf">0.0</span><span class="p">}</span>
<span class="lineno">108</span>    <span class="p">]</span>
<span class="lineno">109</span>    <span class="n">num_decay_params</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">numel</span><span class="p">()</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">decay_params</span><span class="p">)</span>
<span class="lineno">110</span>    <span class="n">num_nodecay_params</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">numel</span><span class="p">()</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">nodecay_params</span><span class="p">)</span>
<span class="lineno">111</span>    <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;num decayed parameter tensors: </span><span class="si">{</span><span class="nb">len</span><span class="p">(</span><span class="n">decay_params</span><span class="p">)</span><span class="si">}</span><span class="s2">, with </span><span class="si">{</span><span class="n">num_decay_params</span><span class="si">:</span><span class="s2">,</span><span class="si">}</span><span class="s2"> parameters&quot;</span><span class="p">)</span>
<span class="lineno">112</span>    <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;num non-decayed parameter tensors: </span><span class="si">{</span><span class="nb">len</span><span class="p">(</span><span class="n">nodecay_params</span><span class="p">)</span><span class="si">}</span><span class="s2">, with </span><span class="si">{</span><span class="n">num_nodecay_params</span><span class="si">:</span><span class="s2">,</span><span class="si">}</span><span class="s2"> parameters&quot;</span><span class="p">)</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-19'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-19'>#</a>
            </div>
            <p>Create AdamW optimizer and use the fused version if it is available </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">114</span>    <span class="n">fused_available</span> <span class="o">=</span> <span class="s1">&#39;fused&#39;</span> <span class="ow">in</span> <span class="n">inspect</span><span class="o">.</span><span class="n">signature</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">optim</span><span class="o">.</span><span class="n">AdamW</span><span class="p">)</span><span class="o">.</span><span class="n">parameters</span>
<span class="lineno">115</span>    <span class="n">use_fused</span> <span class="o">=</span> <span class="n">fused_available</span> <span class="ow">and</span> <span class="n">c</span><span class="o">.</span><span class="n">device_type</span> <span class="o">==</span> <span class="s1">&#39;cuda&#39;</span>
<span class="lineno">116</span>    <span class="n">extra_args</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">fused</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> <span class="k">if</span> <span class="n">use_fused</span> <span class="k">else</span> <span class="nb">dict</span><span class="p">()</span>
<span class="lineno">117</span>    <span class="n">optimizer</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">optim</span><span class="o">.</span><span class="n">AdamW</span><span class="p">(</span><span class="n">optim_groups</span><span class="p">,</span> <span class="n">lr</span><span class="o">=</span><span class="n">c</span><span class="o">.</span><span class="n">learning_rate</span><span class="p">,</span> <span class="n">betas</span><span class="o">=</span><span class="n">c</span><span class="o">.</span><span class="n">betas</span><span class="p">,</span> <span class="o">**</span><span class="n">extra_args</span><span class="p">)</span>
<span class="lineno">118</span>    <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;using fused AdamW: </span><span class="si">{</span><span class="n">use_fused</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
<span class="lineno">119</span>
<span class="lineno">120</span>    <span class="k">return</span> <span class="n">optimizer</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-20'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-20'>#</a>
            </div>
            
        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">123</span><span class="k">def</span> <span class="nf">main</span><span class="p">():</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-21'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-21'>#</a>
            </div>
            <p>Create experiment </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">125</span>    <span class="n">experiment</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;RWKV&quot;</span><span class="p">)</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-22'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-22'>#</a>
            </div>
            <p>Create configs </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">127</span>    <span class="n">conf</span> <span class="o">=</span> <span class="n">Configs</span><span class="p">()</span>
<span class="lineno">128</span>    <span class="nb">print</span><span class="p">(</span><span class="n">conf</span><span class="o">.</span><span class="n">model</span><span class="p">)</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-23'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-23'>#</a>
            </div>
            <p>Override configurations </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">130</span>    <span class="n">experiment</span><span class="o">.</span><span class="n">configs</span><span class="p">(</span><span class="n">conf</span><span class="p">,</span> <span class="p">{</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-24'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-24'>#</a>
            </div>
            <p>Use character level tokenizer </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">132</span>        <span class="s1">&#39;tokenizer&#39;</span><span class="p">:</span> <span class="s1">&#39;character&#39;</span><span class="p">,</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-25'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-25'>#</a>
            </div>
            <p>Prompt separator is blank </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">134</span>        <span class="s1">&#39;prompt_separator&#39;</span><span class="p">:</span> <span class="s1">&#39;&#39;</span><span class="p">,</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-26'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-26'>#</a>
            </div>
            <p>Starting prompt for sampling </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">136</span>        <span class="s1">&#39;prompt&#39;</span><span class="p">:</span> <span class="s1">&#39;It is &#39;</span><span class="p">,</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-27'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-27'>#</a>
            </div>
            <p>Use Tiny Shakespeare dataset </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">138</span>        <span class="s1">&#39;text&#39;</span><span class="p">:</span> <span class="s1">&#39;tiny_shakespeare&#39;</span><span class="p">,</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-28'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-28'>#</a>
            </div>
            <p>Use a context size of <span ><span class="katex"><span aria-hidden="true" class="katex-html"><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord coloredeq eqa" style=""><span class="mord" style="">128</span></span></span></span></span></span> </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">141</span>        <span class="s1">&#39;seq_len&#39;</span><span class="p">:</span> <span class="mi">128</span><span class="p">,</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-29'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-29'>#</a>
            </div>
            <p>Train for <span ><span class="katex"><span aria-hidden="true" class="katex-html"><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">32</span></span></span></span></span> epochs </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">143</span>        <span class="s1">&#39;epochs&#39;</span><span class="p">:</span> <span class="mi">32</span><span class="p">,</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-30'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-30'>#</a>
            </div>
            <p>Batch size <span ><span class="katex"><span aria-hidden="true" class="katex-html"><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord coloredeq eqa" style=""><span class="mord" style="">128</span></span></span></span></span></span> </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">145</span>        <span class="s1">&#39;batch_size&#39;</span><span class="p">:</span> <span class="mi">128</span><span class="p">,</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-31'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-31'>#</a>
            </div>
            <p>Switch between training and validation for <span ><span class="katex"><span aria-hidden="true" class="katex-html"><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">10</span></span></span></span></span> times per epoch </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">148</span>        <span class="s1">&#39;inner_iterations&#39;</span><span class="p">:</span> <span class="mi">10</span><span class="p">,</span>
<span class="lineno">149</span>
<span class="lineno">150</span>        <span class="s1">&#39;rwkv.block_size&#39;</span><span class="p">:</span> <span class="mi">1024</span><span class="p">,</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-32'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-32'>#</a>
            </div>
            <p>model </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">152</span>        <span class="s1">&#39;rwkv.n_layer&#39;</span><span class="p">:</span> <span class="mi">12</span><span class="p">,</span>
<span class="lineno">153</span>        <span class="s1">&#39;rwkv.n_heads&#39;</span><span class="p">:</span> <span class="mi">12</span><span class="p">,</span>
<span class="lineno">154</span>        <span class="s1">&#39;rwkv.n_embd&#39;</span><span class="p">:</span> <span class="mi">768</span>
<span class="lineno">155</span>    <span class="p">})</span>
<span class="lineno">156</span>
<span class="lineno">157</span>    <span class="nb">print</span><span class="p">(</span><span class="n">conf</span><span class="o">.</span><span class="n">model</span><span class="p">)</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-33'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-33'>#</a>
            </div>
            <p>Set models for saving and loading </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">159</span>    <span class="n">experiment</span><span class="o">.</span><span class="n">add_pytorch_models</span><span class="p">({</span><span class="s1">&#39;model&#39;</span><span class="p">:</span> <span class="n">conf</span><span class="o">.</span><span class="n">model</span><span class="p">})</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-34'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-34'>#</a>
            </div>
            <p>Start the experiment </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">162</span>    <span class="k">with</span> <span class="n">experiment</span><span class="o">.</span><span class="n">start</span><span class="p">():</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-35'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-35'>#</a>
            </div>
            <p>Run training </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">164</span>        <span class="n">conf</span><span class="o">.</span><span class="n">run</span><span class="p">()</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-36'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-36'>#</a>
            </div>
            <p> </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">168</span><span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
<span class="lineno">169</span>    <span class="n">main</span><span class="p">()</span></pre></div>
        </div>
    </div>
    <div class='footer'>
        <a href="https://labml.ai">labml.ai</a>
    </div>
</div>
<script src=../interactive.js?v=1"></script>
<script>
    function handleImages() {
        var images = document.querySelectorAll('p>img')

        for (var i = 0; i < images.length; ++i) {
            handleImage(images[i])
        }
    }

    function handleImage(img) {
        img.parentElement.style.textAlign = 'center'

        var modal = document.createElement('div')
        modal.id = 'modal'

        var modalContent = document.createElement('div')
        modal.appendChild(modalContent)

        var modalImage = document.createElement('img')
        modalContent.appendChild(modalImage)

        var span = document.createElement('span')
        span.classList.add('close')
        span.textContent = 'x'
        modal.appendChild(span)

        img.onclick = function () {
            console.log('clicked')
            document.body.appendChild(modal)
            modalImage.src = img.src
        }

        span.onclick = function () {
            document.body.removeChild(modal)
        }
    }

    handleImages()
</script>
</body>
</html>